using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using RideMe.Core;
using ICSharpCode.TextEditor.Added;
using System.IO;
using RideMe.Presentation.Properties;

namespace RideMe.Presentation
{
    public partial class HighlightingOptionsPanel : UserControl
    {
        public HighlightingOptionsPanel()
        {
            InitializeComponent();
            previewLabel.Font = Options.Instance.EditorFont;

            // This is one ugly hack! But the LoadHightlightDocument
            // causes the designer to fail because of how the path 
            // to the .exe is resolved.
            if (Application.ExecutablePath.ToLower().IndexOf(Resources.VisualStudioExe) == -1)
            {
                // Then executing in Visual Studio .NET 
                LoadHightlightDocument();
            }
        }

        private IList<HightlightEntity> _highlightingEntities;

        public IList<HightlightEntity> HightlitingEntites
        {
            get { return _highlightingEntities; }
        }

        private void LoadHightlightDocument()
        {
            _highlightingEntities = EditableHighlightService.GetHighlightItems();
            foreach (HightlightEntity entity in _highlightingEntities)
            {
                displayItemsListBox.Items.Add(entity);
            }
        }

        private void foregroundColorComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            previewLabel.ForeColor = Color.FromName(foregroundColorComboBox.SelectedItem.ToString());

            HightlightEntity entity = displayItemsListBox.SelectedItem as HightlightEntity;

            if (entity == null) return;

            entity.ForeColorName = foregroundColorComboBox.SelectedColor.Name;
        }

        private void backgroundColorComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            previewLabel.BackColor = Color.FromName(backgroundColorComboBox.SelectedItem.ToString());

            HightlightEntity entity = displayItemsListBox.SelectedItem as HightlightEntity;

            if (entity == null) return;

            entity.BackColorName = backgroundColorComboBox.SelectedColor.Name;
        }

        private void displayItemsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            HightlightEntity entity = displayItemsListBox.SelectedItem as HightlightEntity;
            
            if (entity == null) return;

            if (entity.ForeColorName.Length > 0)
            {
                Color foreColor = Color.FromName(entity.ForeColorName);
                foregroundColorComboBox.SelectedColor = foreColor;
            }

            if ((entity.BackColorName != null) && (entity.BackColorName.Length > 0))
            {
                backgroundColorComboBox.Enabled = true;
                Color backColor = Color.FromName(entity.BackColorName);
                backgroundColorComboBox.SelectedColor = backColor;
            }
            else
            {
                backgroundColorComboBox.Enabled = false;
                backgroundColorComboBox.SelectedColor = Color.White;
            }
        }

    }
}
